package com.edou.cms.controller.admin;

import java.util.Map;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.cms.form.CmsArticleTagForm;
import com.edou.cms.entity.CmsArticleTag;
import com.edou.cms.service.ICmsArticleTagService;
import com.edou.base.token.TokenValidate;
import com.edou.base.controller.BaseController;


@Controller("cmsArticleTagController")
@Scope("prototype")
@RequestMapping("/admin/cms/article/tag")
public class CmsArticleTagController extends BaseController {

	@Resource(name = "cmsArticleTagServiceImpl")
	private ICmsArticleTagService cmsArticleTagService;



	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/cms/cms_article_tag_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listCmsArticleTagByPage.htm", method = RequestMethod.POST)
	public void listCmsArticleTagByPage(@ModelAttribute CmsArticleTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			cmsArticleTagService.listCmsArticleTagByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","cmsArticle"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询文章标签关联表时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 保存文章标签关联表
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute CmsArticleTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/cms/cms_article_tag_add");
		Map<String,Object> data = view.getModel();
		if(StringUtils.isNotBlank(model.getItem().getId())){
			CmsArticleTag item = cmsArticleTagService.getCmsArticleTagById(model.getItem().getId());
			data.put("item", item);
		}

		return view;
	}

	/**
	 * 保存文章标签关联表
	 * @return 
	 */
	@RequestMapping(value = "/saveCmsArticleTag.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveRole(@ModelAttribute CmsArticleTagForm model,HttpServletRequest request,HttpSession session){
		try {
			if(StringUtils.isBlank(model.getItem().getId())){
				cmsArticleTagService.add(model.getItem());
			}else{
				cmsArticleTagService.update(model.getItem());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存文章标签关联表时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除CmsArticleTag
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute CmsArticleTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			cmsArticleTagService.delByIds(model.getSelIds().split(","));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除文章标签关联表时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}

	/**
	 * 保存文章标签关联表
	 * @return 
	 */
	@RequestMapping(value = "/saveCmsTagArticleList.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveCmsTagArticleList(@ModelAttribute CmsArticleTagForm model,HttpServletRequest request,HttpSession session){
		try {
			if(StringUtils.isNotBlank(model.getSelIds())){
				cmsArticleTagService.saveTagArticle(model.getSelIds().split(","), model.getCondition().getTagId());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存文章标签关联表时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 修改排序
	 * @param model
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/updateSort.htm",method = RequestMethod.POST)
	public ModelAndView updateSort(@ModelAttribute CmsArticleTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			cmsArticleTagService.updateSort(model.getCondition());
			return ajaxJSON(Status.success);
		} catch (Exception e) {
			String msg = "删除文章标签关联表时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}
}

